//
//  OfficeAutomationController.m
//  XTCloud
//
//  Created by QA on 2017/8/22.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "OfficeAutomationController.h"

#import "OfficeAutomationCell.h"
#import "OAAddressBookCatalogViewController.h"
#import "ApprovalProcessViewController.h"
#import "ECDHomeViewController.h"
#import "OAMyBriefcaseViewController.h"

#import "NSDictionary+DataFormat.h"
#import "UITableView+Custom.h"

#import "WebViewController.h"
#import "GEPushViewControllerHelper.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface OfficeAutomationController ()

@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *listInfoArray;

@end

@implementation OfficeAutomationController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle: @"办公自动化"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.listInfoArray = [NSMutableArray array];
    
    self.tableView.tableFooterView= [[UIView alloc] init];
    self.tableView.separatorColor = [UIColor colorWithWhite:0.90 alpha:1.0];
    
    NSArray *menuAuthorityArray = [self listMenuAuthoritySet];
    for(ModuleAuthorityEntity *entity in menuAuthorityArray){
        [self.listInfoArray addObject:@[entity.title, entity.descripter, entity.icon]];
    }
    
    //没有数据显示提示页面
    if (self.listInfoArray.count <= 0){
        [self.tableView setTableViewNotDataDrawText:@"暂无模块" isShow:YES];
        self.tableView.backgroundColor = [UIColor whiteColor];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//列表权限设置
- (NSArray*)listMenuAuthoritySet
{
    //获取主模块
    NSPredicate *predicateMain = [NSPredicate predicateWithFormat:@"parentId = %@ and moduleType = %@", @0, @5];
    ModuleAuthorityEntity *mainEntity = [ModuleAuthorityEntity MR_findFirstWithPredicate:predicateMain sortedBy:@"order" ascending:YES];
    
    //获取主菜单
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parentId = %@", mainEntity.id];
    NSArray *moduleArray = [ModuleAuthorityEntity MR_findAllSortedBy:@"order" ascending:YES withPredicate:predicate];
    return moduleArray;
}


#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listInfoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *TableSampleIdentifier = @"OfficeAutomationCell";
    OfficeAutomationCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    if (cell == nil)
        cell = [[OfficeAutomationCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
    
    id objectModel = [self.listInfoArray objectAtIndex:indexPath.row];
    if ([objectModel isKindOfClass:[NSArray class]]) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        NSArray *infoArray = objectModel;
        cell.titleLable.text = [infoArray objectAtIndex:0];
        cell.introduceLable.text = [infoArray objectAtIndex:1];
        //cell.iconView.image = [UIImage imageNamed:[infoArray objectAtIndex:2]];
        cell.timeLable.hidden = YES;
        [cell.iconView sd_setImageWithURL:[NSURL URLWithString:[infoArray objectAtIndex:2]] placeholderImage:[UIImage new]];
    }
    
    return cell;
}


#pragma mark
#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 58;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *listArray = [self listMenuAuthoritySet];
    ModuleAuthorityEntity *entity = [listArray objectAtIndex:indexPath.row];
    if(entity.codeType.integerValue == 1){
        //网页模式
        WebPushParameterModel *model = [[WebPushParameterModel alloc] init];
        model.URL = entity.webCode;
        model.Title = entity.title;
        model.CodeType = CodeTypeWeb;
        
        WebViewController *viewController = [[WebViewController alloc] initWithPushParameterModel:model];
        [self.navigationController pushViewController:viewController animated:YES];
    } else {
        //原生APP模式
        [GEPushViewControllerHelper pushViewControllerHomePageCode:entity.nativeCode navigation:self.navigationController];
    }
}

@end
